package com.example.gybicycle.service;

import  com.alibaba.fastjson.JSON;


import com.example.gybicycle.common.util.QRCodeUtil;
import com.example.gybicycle.common.util.ZipUtil;
import com.example.gybicycle.entity.Bike;
import com.example.gybicycle.entity.BikeCode;
import com.example.gybicycle.entity.Lock;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

@Service
public class FileService {
    @Value("${server.port}")
    public String port;
    @Value("${file.path}")
    public String path;
    @Value("${file.url}")
    public String  urlHead;
    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.secret}")
    private String secret;

    public String createBikeQRCode(Bike entity){
        try {
            String url="https://www.alexcycling.asia/scanCode?isScanCode=1&id="+entity.getId();

//            code.setIsScanCode(1);
            String img="/rqcode/bike/"+entity.getId()+".png";
//            QRCodeMsg msg=new QRCodeMsg(entity);
            String file= QRCodeUtil.createImg(url,entity.getId(),path+img);
//            String file= QRCodeUtil.createImg(JSON.toJSONString(code, SerializerFeature.WriteMapNullValue),entity.getId(),path+img);
            if(file==null)
                return null;
            else
                return urlHead+img;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public String createLockQRCode(Lock entity){
        try {
            String img="/rqcode/lock/"+entity.getId()+".png";
//            QRCodeMsg msg=new QRCodeMsg(entity);
            String file= QRCodeUtil.createImg(JSON.toJSONString(entity),entity.getId(),path+img);
            if(file==null)
                return null;
            else
                return urlHead+img;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public String downLoadBikeQRCode(List<String> paths){
        paths=ZipUtil.upPaths(paths,urlHead,path);
        String zipName=ZipUtil.zipfiles(paths,path);
        return path+zipName;
    }
    public String downLoadBikeQRCode2(List<String> paths){
        paths=ZipUtil.upPaths(paths,urlHead,path);
        String zipName=ZipUtil.zipfiles(paths,path);
        if(zipName==null)
            return null;
        return  urlHead+zipName;
    }

  /*  public String createPointQRCode(PatrolPoints entity) {
        try { String img="/rqcode/point/"+entity.getId()+".png";
            QRCodeMsg msg=new QRCodeMsg(entity);

            String file=QRCodeUtil.createImg(JSON.toJSONString(msg, SerializerFeature.WriteMapNullValue),entity.getName(),path+img);
            if(file==null)
                return null;
            else
                return urlHead+img;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }*/
    /*public String createFocusQRCode(FocusArea entity) {
        try {
            String img="/rqcode/focus/"+entity.getId()+".png";
            QRCodeMsg msg=new QRCodeMsg(entity);

            String file=QRCodeUtil.createImg(JSON.toJSONString(msg, SerializerFeature.WriteMapNullValue),entity.getName(),path+img);
            if(file==null)
                return null;
            else
                return urlHead+img;
//        String img= QRCodeGenerator.createQRcode(JSON.toJSONString(entity),path,"/rqcode/focus/"+entity.getId()+".png");
//        return urlHead+img;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }*/

  /* public String createMerchantCode(Merchant entity){
        String img="/rqcode/store/"+entity.getId()+".png";
        String txt="https://xfcs.everbf.com/mini?routerId="+entity.getId();
        String file=QRCodeUtil.createImg(txt,entity.getName(),path+img);
        if(file==null)
            return null;
        else
            return urlHead+img;

    }*/
    public void saveFailPayInfo(String data){
        String paths=path+"/pay.txt";
        data=data+"\n";
        try {
            Path filepath= Paths.get(paths);
            if (!Files.exists(filepath)) {
                Files.createFile(filepath);
            }
            Files.write(filepath, data.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void saveLockInfo(String data){
        String paths=path+"/lock.txt";
        data=data+"\n";
        try {
            Path filepath= Paths.get(paths);
            if (!Files.exists(filepath)) {
                Files.createFile(filepath);
            }
            Files.write(filepath, data.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}